﻿@{
    Layout = "~/Views/Shared/_Layout_lay_admin.cshtml";
}
<style>
    body {
        background-color: #ffffff;
    }

    .custom-tree-item-clicked {
        color: #ff5722
    }
</style>


<div class="layui-form layuimini-form">

    <div class="layui-form-item" style="display:none">
        <label class="layui-form-label ">代码</label>
        <div class="layui-input-block">
            <input type="text" name="ProjectId" id="ProjectId" class="layui-input">
            <input type="text" name="CompanyType" value="FormelD" id="CompanyType" class="layui-input">
            <input type="text" name="User_Id" value="" id="User_Id" class="layui-input">
        </div>
    </div>
    <div class="layui-row">
        <div class="layui-col-xs5">
            <div class="layui-form-item">
                <label class="layui-form-label required">@ViewBag.SelectUser</label>
                <div class="layui-input-block" style=" border: solid 1px #27AAA5;min-height:500px;margin-right:4px; overflow-y:scroll; ">

                    <div id="test9" class="demo-tree demo-tree-box" style="height: 100%; max-height:500px;  "></div>
                </div>
            </div>
            <div class="layui-form-item" style="display:none;">
                <label class="layui-form-label ">@ViewBag.Selected</label>
                <div class="layui-input-block">
                    <input type="text" readonly="readonly" id="SelectedUser" name="SelectedUser" value="" autocomplete="off" class="layui-input">
                </div>
                <script type="text/html" id="currentTableBarUser">
                    <a ex_btn="ex_delete" class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"> @ViewBag.Delete</a>
                </script>
            </div>
        </div>
        <div class="layui-col-xs7">
            已添加的用户
            <table class="layui-hide formeldboard" id="currentTableUser" lay-filter="currentTableFilter"></table>

        </div>
    </div>

    <div class="layui-form-item">

    </div>
    <div class="layui-form-item" style="display:none;">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script>
    var tree;

    var nodeName = "";

    var companytype = GetQueryString("companytype");
    var projectId = GetQueryString("ProjectId");
    var projectNumber = GetQueryString("ProjectNumber");

    var tableuser;
    var reloadTable = function (sortField, sortType) {
        var keyword = $("#keyword").val();

        tableuser.reload('currentTableUser', {
            where: { //设定异步数据接口的额外参数，任意设
                keyword: keyword,
                projectId:projectId,
                sortField: sortField,
                sortType: sortType
            }
        });
    };

    var treeloadload = function () {

        $.ajax({
            type: "GET",
            url: '/Base_Organization/SearchByCompany?keyword=' + companytype,
            dataType: 'json',
            success: function (_data) {
                //debugger
                //, url: '/Base_Role/Search'
                //开启节点操作图标
                tree.render({
                    elem: '#test9'
                    , data: _data
                    //, edit: ['add', 'del'] //操作节点的图标
                    , click: function (obj) {
                        //layer.msg(obj.data.id + "," + obj.data.title + "," + obj.data.attrName);
                        var isuser = obj.data.attrName;

                        if (isuser == "U") {
                            $("#User_Id").val(obj.data.id);
                            $("#SelectedUser").val(obj.data.title);
                        } else {
                            $("#User_Id").val("");
                            $("#SelectedUser").val("");
                        }
                        addUser(obj.data.id, obj.data.title);


                    },
                    text: {
                        defaultNodeName: nodeName //节点默认名称
                        , none: '' //数据为空时的提示文本
                    },
                    limitNodeAddLevel: 0
                    , customOperate: true
                    , operate: function (obj, callback) {

                        //layer.msg(id);

                    }
                });
            }
        });

        var index0pen;

    }

    function addUser(_userid, _username) {//,projectid,projectNumber

        var _projectId = GetQueryString("ProjectId");
        var _projectNumber = GetQueryString("ProjectNumber");

        $.ajax({
            type: "POST",
            url: '/ProjectInfoUser/Create2',
            dataType: 'json',
            data: { UserId: _userid,UserName: _username,ProjectId:_projectId,ProjectNumber:_projectNumber,UserType:'custom'},
            success: function (obj) {
                //debugger
                //, url: '/Base_Role/Search'
                //开启节点操作图标
                if (obj.Code=="m001") {
                    reloadTable('CreateTime','asc');
                } else {
                     layer.alert(obj.Message);
                }

            }
        });

    }



    layui.use(['tree', 'table','form'], function () {
        window.tree = layui.tree;
        var form = layui.form,
            layer = layui.layer,

            $ = layui.$;
        window.tableuser = layui.table;

        treeloadload();


        $("#ProjectId").val(GetQueryString("ProjectId"));

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            if ($("#User_Id").val() == "") {
                layer.alert('@ViewBag.Organization_UserNotNull');
                return;
            }

            $.ajax({
                url: '/Base_Organization/BindUserOrganization',
                type: 'post',
                dataType: 'text',
                data: data.field,
                success: function (resp) {
                    var json = eval('(' + resp + ')');
                    var index = layer.alert(json.Message, function () {
                         reloadTable('CreateTime', 'asc');
                        // 关闭弹出层
                        //layer.close(index);
                        //parent.reloadTable();
                        //var iframeIndex = parent.layer.getFrameIndex(window.name);
                        //parent.layer.close(iframeIndex);
                    });
                    //layer.alert(json.Message);
                    //reloadTable();
                },
                error: function (resp) {
                    layer.alert("对不起出错了");
                }
            });

            return false;
        });

        tableuser.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('@ViewBag.AreYouSure', function (index) {
                    var strIds = data.ProjectinfoUserId;
                    layer.close(index);
                    $.ajax({
                        url: '/ProjectInfoUser/ActivateSwich',
                        type: 'post',
                        dataType: 'text',
                        data: { "primarykeys": strIds },
                        success: function (resp) {
                            //var json = eval('(' + resp + ')');
                            //layer.alert(json.Message);
                            reloadTable('CreateTime', 'asc');
                        },
                        error: function (resp) {
                            //layer.alert('@ViewBag.PostError');
                        }
                    })
                })
            }
        });



        var _url = '/ProjectInfoUser/Search';
        tableuser.render({
            elem: '#currentTableUser',
            url: _url,
            //toolbar: '#toolbarDemo',
            //defaultToolbar: [],
             where: { //设定异步数据接口的额外参数，任意设
                 keyword: '',
                 projectId: projectId,
                 UserType:'custom',
                sortField: 'CreateTime',
                sortType: 'asc'
            },
            cols: [[
                //{ type: "checkbox", width: 50 },
                //{ field: 'ProjectinfoUserId', width: 200, title: 'ProjectinfoUserId' },
                //{ field: 'ProjectId', width: 200, title: 'ProjectId' },
                //{ field: 'ProjectNumber', width: 140, title: '项目' },
                //{ field: 'UserId', width: 200, title: 'UserId' },
                { field: 'UserName', title: '姓名' },
                //{
                //    field: 'UploadData', width: 80, title: '上传', templet: function (d) {
                //        var html = "<input type='checkbox' lay-skin='primary' lay-filter='changeUploadData' class='checkboxIsSelected' ProjectinfoUserId='"+d.ProjectinfoUserId+"'  value='1' ";
                //        if(d.UploadData == 1){
                //            html += " checked ";
                //        }
                //        html += " />";
                //        return html;
                //    }
                //},
                { field: 'Readonly', width: 80, title: '查看',templet: function (d) {
                        if (d.Readonly == '1') {
                            return "允许";
                        }
                        if (d.Readonly == '0') {
                            return "-"
                        }
                        return d.Readonly;
                     }
                },
                //{ field: 'PrimaryKeyUser',  title: '是负责人',templet: function (d) {
                //    var html = "<input type='checkbox' lay-skin='primary' lay-filter='changePrimaryKeyUser' class='checkboxIsSelected' ProjectinfoUserId='"+d.ProjectinfoUserId+"'  value='1' ";
                //        if(d.PrimaryKeyUser == 1){
                //            html += " checked ";
                //        }
                //        html += " />";
                //        return html;
                //     } },
                { title: '操作', minWidth:60, toolbar: '#currentTableBarUser', align: "center" ,fixed: 'right'},
                ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
             done: function (obj) {
                 form.on('checkbox(changeUploadData)', function (data) {
                     //var $checkbox = $(this);
                     var UploadData = data.elem.checked;
                     var ProjectinfoUserId = data.elem.getAttribute('ProjectinfoUserId');


                     $.ajax({
                        type: "POST",
                        url: '/ProjectInfoUser/EditData',
                        dataType: 'json',
                        data: { ProjectinfoUserId: ProjectinfoUserId,UploadData: UploadData,ModifyColumn:'UploadData',UserType:'custom'},
                        success: function (obj) {
                            //debugger
                            //, url: '/Base_Role/Search'
                            //开启节点操作图标
                            if (obj.Code=="m002") {
                                 layer.alert(obj.Message);
                            }
                        }
                     });

                 });

                 form.on('checkbox(changePrimaryKeyUser)', function (data) {
                     //var $checkbox = $(this);
                     var PrimaryKeyUser = data.elem.checked;
                     var ProjectinfoUserId = data.elem.getAttribute('ProjectinfoUserId');
                     $.ajax({
                         type: "POST",
                         url: '/ProjectInfoUser/EditData',
                         dataType: 'json',
                         data: { ProjectinfoUserId: ProjectinfoUserId, PrimaryKeyUser: PrimaryKeyUser, ModifyColumn: 'PrimaryKeyUser' ,UserType:'custom'},
                         success: function (obj) {
                             //debugger
                             //, url: '/Base_Role/Search'
                             //开启节点操作图标
                             if (obj.Code == "m002") {
                                 layer.alert(obj.Message);
                             }
                         }
                     });
                 });

                }
        });
        //reloadTable('ProjectinfoUserId','asc');

    });
</script>
